﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Myproject.DTO;
using Myproject.DataAccess.factory;
using Myproject.DataCRM;
using Myproject.Enum;

namespace Myproject.DataAccess.entity
{
    public class UserProcess : EntityProcess
    {
        public void addUser(User TUser,Enum.transactionEnum TransactionInfo)
        {
            using (DBFactoryInstance dbInstance = DBFactoryInstance.DbInstance)
            {
                switch(TransactionInfo)
                {
                    case transactionEnum.End:
                    case transactionEnum.StartToEnd:
                    {
                        AddUserFunction(TUser, dbInstance);
                        TransactionInstance.Complete();
                        TransactionInstance.Dispose();
                        break;
                    }
                    case transactionEnum.Running:
                    case transactionEnum.Start:
                    {
                        AddUserFunction(TUser, dbInstance);
                        break;
                    }
                }
            }
        }

        private void AddUserFunction(User TUser, DBFactoryInstance dbInstance)
        {
            if (TransactionInstance == null) TransactionInstance = new System.Transactions.TransactionScope();
            dbInstance.AddToT_USER(TUser);
            dbInstance.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
        }

        public void updateUser(User TUser, Enum.transactionEnum TransactionInfo)
        {
            using (DBFactoryInstance dbInstance = DBFactoryInstance.DbInstance)
            {
                switch(TransactionInfo)
                {
                    case transactionEnum.StartToEnd:
                    case transactionEnum.End:
                    {
                        UpdateUserFunction(TUser, dbInstance);
                        TransactionInstance.Complete();
                        TransactionInstance.Dispose();
                        break;
                    }
                    case transactionEnum.Start:
                    case transactionEnum.Running:
                    {
                        UpdateUserFunction(TUser, dbInstance);
                        break;
                    }
                }
            }
        }

        private void UpdateUserFunction(User TUser, DBFactoryInstance dbInstance)
        {
            if (TransactionInstance == null) TransactionInstance = new System.Transactions.TransactionScope();
            T_USER userInstance = dbInstance.T_USER.SingleOrDefault(c => c.userid == TUser.userid);
            userInstance.username = TUser.username;
            userInstance.loginname = TUser.loginname;
            userInstance.password = TUser.password;
            userInstance.sex = TUser.sex;
            userInstance.address = TUser.address;
            userInstance.memo = TUser.memo;
            userInstance.status = TUser.status;
            userInstance.updatetime = DateTime.Now;
            userInstance.updater = TUser.updater;
            dbInstance.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
        }

        public void deleteUser(User TUser, Enum.transactionEnum TransactionInfo)
        {
            using (DBFactoryInstance dbInstance = DBFactoryInstance.DbInstance)
            {
                switch(TransactionInfo)
                {
                    case transactionEnum.StartToEnd:
                    case transactionEnum.End:
                    {
                        DeleteUserFunction(TUser, dbInstance);
                        TransactionInstance.Complete();
                        TransactionInstance.Dispose();
                        break;
                    }
                    case transactionEnum.Start:
                    case transactionEnum.Running:
                    {
                        DeleteUserFunction(TUser, dbInstance);
                        break;
                    }
                }
            }
        }

        private void DeleteUserFunction(User TUser, DBFactoryInstance dbInstance)
        {
            if (TransactionInstance == null) TransactionInstance = new System.Transactions.TransactionScope();
            dbInstance.DeleteObject(dbInstance.T_USER.SingleOrDefault(c => c.userid == TUser.userid));
            dbInstance.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
        }

        public List<User> GetUserListByUser(User TUser)
        {
            List<User> result = new List<User>();
            using (DBFactoryInstance dbInstance = DBFactoryInstance.DbInstance)
            {
                var list = from u in dbInstance.T_USER
                           where (TUser.userid== null || TUser.userid==u.userid) &&
                                 (TUser.username == null || u.username.Contains(TUser.username)) &&
                                 (TUser.loginname == null ||  u.loginname.Contains(TUser.loginname)) &&
                                 (TUser.sex == null || TUser.sex == u.sex) &&
                                 (TUser.address == null || u.address.Contains(TUser.address)) &&
                                 (TUser.memo == null || u.memo.Contains(TUser.memo)) &&
                                 (TUser.status == null || TUser.status == u.status) &&
                                 (TUser.StartCreateTime == null || TUser.StartCreateTime <= u.createtime) &&
                                 (TUser.EndCreateTime == null || TUser.EndCreateTime >= u.createtime) 
                           select u;
                foreach (var listvalue in list)
                {
                    result.Add(new User 
                    {
                        userid=listvalue.userid,
                        username=listvalue.username,
                        loginname=listvalue.loginname,
                        password=listvalue.password,
                        sex=listvalue.sex,
                        address=listvalue.address,
                        memo=listvalue.memo,
                        status = listvalue.status,
                        createtime=listvalue.createtime,
                        updatetime=listvalue.updatetime,
                        author=listvalue.author,
                        updater=listvalue.updater
                    });
                }
            }
            
            return result;
        }

    }
}
